package cn.itcast.ks.service.impl;

import cn.itcast.ks.common.AppHttpCodeEnum;
import cn.itcast.ks.common.ResponseResult;
import cn.itcast.ks.common.threadlocal.UserThreadLocalUtils;
import cn.itcast.ks.mapper.AssignmentMapper;
import cn.itcast.ks.pojo.Vos.AssignmentVo;
import cn.itcast.ks.pojo.Vos.NotWorkVo;
import cn.itcast.ks.pojo.dtos.AssignmentDto;
import cn.itcast.ks.pojo.dtos.NotWorkDto;
import cn.itcast.ks.pojo.dtos.ShowWorkDto;
import cn.itcast.ks.pojo.dtos.WorkDto;
import cn.itcast.ks.pojo.entity.Assignment;
import cn.itcast.ks.pojo.entity.User;
import cn.itcast.ks.pojo.entity.UserStudentAssignment;
import cn.itcast.ks.service.IAssignmentService;
import cn.itcast.ks.service.IClassService;
import cn.itcast.ks.service.IUserService;
import cn.itcast.ks.service.IUserStudentAssignmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wjs
 * @version 1.0
 * @date 2021/12/5 0005 下午 4:05
 */
@Slf4j
@Service
public class AssignmentServiceImpl extends ServiceImpl<AssignmentMapper, Assignment> implements IAssignmentService {

    @Autowired
    private IUserStudentAssignmentService userStudentAssignmentService;
    @Autowired
    private IUserService userService;

    @Override
    public ResponseResult findStudentWork(ShowWorkDto dto) {
        // 将传输的时间转换为字符串
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String newTime = simpleDateFormat.format(dto.getTime());

        AssignmentVo assignmentVo = new AssignmentVo();
        User user = UserThreadLocalUtils.getUser();
        assignmentVo.setUid(user.getId());
        assignmentVo.setName(user.getName());

        LambdaQueryWrapper<UserStudentAssignment> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserStudentAssignment::getUserId,user.getId())
                .eq(UserStudentAssignment::getCreatedOn,newTime)
                .eq(UserStudentAssignment::getAssignmentType,dto.getType())
                .isNull(UserStudentAssignment::getIsDeleted);
        List<UserStudentAssignment> list = userStudentAssignmentService.list(lqw);
        //System.err.println(list);
        List<Map> maps = list.stream().map((userStudentAssignment) -> {
            Map map = new HashMap();
            map.put("workname", userStudentAssignment.getAssignmentName());
            map.put("isComplete", userStudentAssignment.getStatus());
            map.put("id", userStudentAssignment.getId());
            return map;
        }).collect(Collectors.toList());
        assignmentVo.setPracticeList(maps);
        System.err.println(assignmentVo);

        return ResponseResult.okResult(assignmentVo);
    }

    @Override
    public ResponseResult showWorks(ShowWorkDto showWorkDto) {
        // 将传输的时间转换为字符串
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String newTime = simpleDateFormat.format(showWorkDto.getTime());

        User user = UserThreadLocalUtils.getUser();
        if (user == null || user.getRoleId() != 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        //根据userId(老师)获取班级信息
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getClassId,user.getClassId())
                .eq(User::getRoleId,2)
                .eq(User::getIsDeleted,0);
        List<User> users = userService.list(lqw);

        List<AssignmentVo> assignmentVos = new ArrayList<>();
        for (User u : users) {
            //System.err.println(u);
            AssignmentVo assignmentVo = new AssignmentVo();
            assignmentVo.setUid(u.getId());
            assignmentVo.setName(u.getName());
            List<Map> maps = new ArrayList<>();
            
            LambdaQueryWrapper<UserStudentAssignment> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(UserStudentAssignment::getCreatedOn,newTime)
                    .eq(UserStudentAssignment::getAssignmentType,showWorkDto.getType())
                    .eq(UserStudentAssignment::getUserId,u.getId())
                    .isNull(UserStudentAssignment::getIsDeleted);
            List<UserStudentAssignment> list = userStudentAssignmentService.list(lqw1);
            //System.err.println("list:");
            //System.out.print(list);

            for (int i = 0; i < list.size(); i++) {
                Map map = new HashMap();
                UserStudentAssignment userStudentAssignment = list.get(i);
                //System.err.println(userStudentAssignment);
                map.put("workname",userStudentAssignment.getAssignmentName());
                map.put("isComplete",userStudentAssignment.getStatus());
                map.put("id",userStudentAssignment.getId());
                //System.err.println(map);
                maps.add(map);
            }
            //System.err.println(maps);
            assignmentVo.setPracticeList(maps);
            //System.err.println(assignmentVo);
            assignmentVos.add(assignmentVo);
        }
        //System.err.println(assignmentVos);
        return ResponseResult.okResult(assignmentVos);
    }

    @Override
    public ResponseResult publishWork(WorkDto workDto) {

        User user = UserThreadLocalUtils.getUser();
        if (user.getRoleId() != 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        Assignment assignment = new Assignment();

        assignment.setUserId(user.getId());
        assignment.setName(workDto.getName());
        assignment.setMemo(workDto.getMemo());
        assignment.setType(workDto.getType());
        assignment.setCreatedOn(workDto.getCreatedOn());
        this.save(assignment);

        //根据userId(老师)获取班级信息
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getClassId,user.getClassId())
                .eq(User::getRoleId,2)
                .eq(User::getIsDeleted,0);
        List<User> users = userService.list(lqw);

        List<UserStudentAssignment> userStudentAssignments = users.stream().map((u)->{
            UserStudentAssignment userStudentAssignment = new UserStudentAssignment();
            userStudentAssignment.setUserId(u.getId());
            userStudentAssignment.setAssignmentId(assignment.getId());
            userStudentAssignment.setAssignmentName(assignment.getName());
            userStudentAssignment.setAssignmentType(assignment.getType());
            userStudentAssignment.setStatus(1);
            userStudentAssignment.setCreatedOn(assignment.getCreatedOn());
            return userStudentAssignment;
        }).collect(Collectors.toList());

        userStudentAssignmentService.saveBatch(userStudentAssignments);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findNotWorkByTime(NotWorkDto notWorkDto) {

        // 将传输的时间转换为字符串
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = simpleDateFormat.format(notWorkDto.getStartTime());
        String endTime = simpleDateFormat.format(notWorkDto.getEndTime());

        User user = UserThreadLocalUtils.getUser();
        if (user.getRoleId() != 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        //根据userId(老师)获取班级信息
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getClassId,user.getClassId())
                .eq(User::getRoleId,2)
                .eq(User::getIsDeleted,0);
        List<User> users = userService.list(lqw);

        List<NotWorkVo> notWorkVos = new ArrayList<>();
        for (User u : users) {
            NotWorkVo notWorkVo = new NotWorkVo();
            notWorkVo.setName(u.getName());
            LambdaQueryWrapper<UserStudentAssignment> lqw1 = new LambdaQueryWrapper<>();
            lqw1.between(UserStudentAssignment::getCreatedOn,startTime,endTime)
                    .eq(UserStudentAssignment::getStatus,1)
                    .eq(UserStudentAssignment::getAssignmentType,notWorkDto.getType())
                    .isNull(UserStudentAssignment::getIsDeleted)
                    .eq(UserStudentAssignment::getUserId,u.getId());
            //System.err.println(u.getId()+"...."+notWorkDto.getType()+"...."+startTime+"...."+endTime);
            //int count = userStudentAssignmentService.count(lqw1);
            List<UserStudentAssignment> list = userStudentAssignmentService.list(lqw1);
            int count = list.size();
            System.out.println(count);
            notWorkVo.setCount(count);
            notWorkVo.setUserId(u.getId());

            notWorkVos.add(notWorkVo);
        }

        return ResponseResult.okResult(notWorkVos);
    }
}
